/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.store.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.javaeye.store.dto.MaterialsBatchDetail;

public class MaterialsBatchDetailDAOHibernate extends HibernateDaoSupport implements MaterialsBatchDetailDAO {

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialsBatchDetail> getBatchInfoByMaterialsId(int materialsId) {
		final String queryString = "from MaterialsBatchDetail d where d.materialsId=? and d.number>d.outNumber order by d.batchNo";
		List result = getHibernateTemplate().find(queryString, materialsId);
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialsBatchDetail> getNotFinishBatchInfo(int materialsId) {
		final String queryString = "from MaterialsBatchDetail d where d.materialsId=? and d.planNumber>d.number order by d.batchNo";
		List result = getHibernateTemplate().find(queryString, materialsId);
		return result;
	}
	
	/**
	 * 返回没有完成的产品计划批号
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<String> getNotFinishProductBatchInfo() {
		final String queryString = "select distinct d.batchNo from MaterialsBatchDetail d where d.planNumber>d.number and d.type=0 order by d.batchNo";
		List result = getHibernateTemplate().find(queryString);
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialsBatchDetail> getDefectiveMaterialsBatchInfo(int materialsId) {
		final String queryString = "from MaterialsBatchDetail d where d.materialsId=? and d.type=1 and d.planNumber > 0 and d.planNumber > d.number order by d.batchNo";
		List result = getHibernateTemplate().find(queryString, materialsId);
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public double getMaxPrice(int materialsId) {
		//查询有退货，且未退完的物料的最大采购价格
		final String queryString = "select max(d.price) from MaterialsBatchDetail d where d.materialsId=? and d.planNumber > 0 and d.planNumber > d.number";
		List result = getHibernateTemplate().find(queryString, materialsId);
		double price = 0;
		if (result.size() > 0) {
			price = (Double)result.get(0);
		}
		return price;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialsBatchDetail> getNotEmptyMeterialsBatchInfo(int materialsId) {
		final String queryString = "from MaterialsBatchDetail d where d.materialsId=? and d.inNumber>d.outNumber order by d.batchNo";
		List result = getHibernateTemplate().find(queryString, materialsId);
		return result;
	}
	
	@Override
	public void save(MaterialsBatchDetail detail) {
		getHibernateTemplate().saveOrUpdate(detail);
		logger.debug("MaterialsBatchDetail id set to: " + detail.getId());
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public MaterialsBatchDetail getBatchDetail(int materialsId, String batchNo) {
		final String queryString = "from MaterialsBatchDetail d where d.materialsId=? and d.batchNo=?";
		List result = getHibernateTemplate().find(
				queryString, new Object[] {materialsId, batchNo});
		if (result == null || result.size() == 0) {
			return null;
		}
		return (MaterialsBatchDetail)result.get(0);
	}
	
	public MaterialsBatchDetail getBatchInfo(int batchDetailId) {
		return (MaterialsBatchDetail)getHibernateTemplate().get(MaterialsBatchDetail.class, batchDetailId);
	}
	
	public void removeDetail(final int materialsId) {
		HibernateCallback action = new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				final String queryString = "delete from MaterialsBatchDetail d where d.materialsId=" + materialsId;
				Query query=session.createQuery(queryString);
				int size = query.executeUpdate();
				return size;
			}
		};
		getHibernateTemplate().execute(action);
	}

}
